package hui.com.controller.wms;

import hui.com.model.res.MyResponse;
import hui.com.service.WarehouseService;
import hui.com.authority.Authority;
import hui.com.model.entity.WarehouseEntity;
import hui.com.model.req.RequestWarehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@CrossOrigin(origins = "*")//处理跨源资源共享
@RequestMapping("/public/Warehouse")
@Controller
public class Warehouse {
    @Autowired
    @Resource
    WarehouseService warehouseService;


    /**
     * 全局模糊查询
     *
     * @param warehouse 对象
     * @return RequestGetLikeSales
     */
    @RequestMapping("/warehouseADD")
    @ResponseBody
    @Authority("warehouse_add")
    public Object addSalesList(@RequestBody(required = false) RequestWarehouse warehouse) {
        if (warehouse == null) {
            return MyResponse.builder().code(202).msg("没有接收到参数，请查看格式是否正确").build();
        }
        if (warehouse.getType().isEmpty() || addSalesListType(warehouse.getType())) {
            return MyResponse.builder().code(202).msg("type没有接收到明确类型，请检查是否正确。single or list。当前传值" + warehouse.getType()).build();

        }
        return warehouseService.addStorageLocationList(warehouse);

    }

    public Boolean addSalesListType(String val) {
        return !val.equals("listX") && !val.equals("listY") && !val.equals("listZ") && !val.equals("list") && !val.equals("single");
    }

    /**
     * 所有库位名称
     *
     * @return RequestGetLikeSales
     */
    @RequestMapping("/getCodeNameArray")
    @ResponseBody
    @Authority("warehouse_show")
    public Object getCodeNameArray() {
        return MyResponse.builder().code(200).data(warehouseService.getCodeNameArray()).build();

    }

    /**
     * 查询指定库的库位信息
     *
     * @param warehouse
     * @return
     */
    @RequestMapping("/getListCodeNameStorageLocation")
    @ResponseBody
    @Authority("warehouse_show")
    public Object getListCodeNameStorageLocation(@RequestBody(required = false) WarehouseEntity warehouse) {
        if (warehouse == null) {
            MyResponse.builder().code(201).msg("参数不能为空").build();
        }

        return MyResponse.builder().code(200).data(warehouseService.getListCodeNameStorageLocation(warehouse)).build();
    }

    /**
     * 查询指定库的库位信息
     *
     */
    @RequestMapping("/getListCodeNameXYZStorageLocation")
    @ResponseBody
    @Authority("warehouse_show")
    public Object getListCodeNameXYZStorageLocation(@RequestBody(required = false) WarehouseEntity warehouse) {
        if (warehouse == null || (warehouse.getX() == null && warehouse.getY() == null && warehouse.getZ() == null)) {
            MyResponse.builder().code(201).msg("参数不能为空").build();
        }

        return MyResponse.builder().code(200).data(warehouseService.getListCodeNameXYZStorageLocation(warehouse)).build();

    }
}
